﻿using System;
using System.IO;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;

namespace PickGold.Nulls
{
	/// <summary>
	/// NullStream
	/// </summary>
	public sealed class NullStream : Stream
	{
		class AsyncResult : IAsyncResult
		{
			public object AsyncState
			{
				get;
				set;
			}

			public WaitHandle AsyncWaitHandle
			{
				get;
				set;
			}

			public bool CompletedSynchronously
			{
				get;
				set;
			}

			public bool IsCompleted
			{
				get;
				set;
			}
		}

		/// <summary>
		/// 唯一实例
		/// </summary>
		public readonly static Stream Value = new NullStream();

		private NullStream()
		{
			if (Value != null)
				throw new InvalidOperationException();
		}

		public override bool CanRead
		{
			get
			{
				return true;
			}
		}

		public override bool CanSeek
		{
			get
			{
				return true;
			}
		}

		public override bool CanWrite
		{
			get
			{
				return true;
			}
		}

		public override void Flush()
		{
		}

		public override long Length
		{
			get
			{
				return 0L;
			}
		}

		public override long Position
		{
			get
			{
				return 0L;
			}
			set
			{
			}
		}

		public override int ReadByte()
		{
			return -1;
		}

		public override int Read(byte[] buffer, int offset, int count)
		{
			return 0;
		}

		public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
		{
			var ar = new AsyncResult { AsyncState = state, CompletedSynchronously = true, IsCompleted = true, };
			if (callback != null)
				callback(ar);
			return ar;
		}

		public override int EndRead(IAsyncResult asyncResult)
		{
			return 0;
		}

		public override long Seek(long offset, SeekOrigin origin)
		{
			return 0L;
		}

		public override void SetLength(long value)
		{
		}

		public override void WriteByte(byte value)
		{
		}

		public override void Write(byte[] buffer, int offset, int count)
		{
		}

		public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
		{
			var ar = new AsyncResult { AsyncState = state, CompletedSynchronously = true, IsCompleted = true, };
			if (callback != null)
				callback(ar);
			return ar;
		}

		public override void EndWrite(IAsyncResult asyncResult)
		{
		}
	}
}
